// pages/message/message.js
var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //getAllMessage
    var that = this;
    that.setData({
      code: codeName
    })
    that.getlodeTap();
  },

  getlodeTap: function (e) {
    var that = this;
    utils.http(httpUrl + "getAllMessage", {
      code: codeName
    }, function (res) {
      console.log(res);
      var messagelistpost=[]
      for (var index in res.messagelist) {
        var messagepost = {
          code: res.messagelist[index].code,
          commentlist: res.messagelist[index].commentlist,
          totalnum: res.messagelist[index].commentlist.length,
          content: res.messagelist[index].content,
          createDate: res.messagelist[index].createTime,
          createTime: res.messagelist[index].createTime,
          id: res.messagelist[index].id,
          messagecount: res.messagelist[index].messagecount,
          title: res.messagelist[index].title,
          user: res.messagelist[index].user,
          userid: res.messagelist[index].userid
        }

        messagelistpost.push(messagepost);
      }
      that.setData({
        dataList:messagelistpost
      })
      console.log(that.data.dataList)
    })
  },
  //发表留言求助页面
  oNappealTap: function (e) {
    wx.navigateTo({
      url: '../message/message_appeal/message_appeal',
    })
  },

  //键盘输入是触发按钮
  oNbindinputTap: function (e) {
    var valueName = e.detail.value;
    if (valueName.length == 0) {
      this.data.isSubmit = false;
    } else {
      this.data.isSubmit = true;
    }
    this.setData({
      isSubmit: this.data.isSubmit,
      valueName: valueName
    })

  },



  //点击不同用户回复信息
  onchangeTap: function (e) {
    var that = this;
    var messageid = e.currentTarget.dataset.messageid;
    var parentusername = e.currentTarget.dataset.parentusername;
    var nickname = e.currentTarget.dataset.nickname;
    console.log(nickname);
    var parentid = e.currentTarget.dataset.parentid;
    var parentuserid = e.currentTarget.dataset.parentuserid;
    var username = e.currentTarget.dataset.username;   //要回复的username
    console.log(e);
    var userid = e.currentTarget.dataset.userid;
    var id = e.currentTarget.dataset.id;
    if (parentuserid == undefined) {
      that.setData({
        parentid: id,
        parentuserid: userid,
      })
    } else {
      that.setData({
        parentid: parentid,
        parentuserid: parentuserid,
      })
    }

    this.setData({
      ishis: true,
      messageid: messageid,
      parentusername: parentusername,
      nickname: nickname,

      username: username,
      valueName: ""
    })
  },

  //提交回复
  bindsubmit: function (e) {
    var that = this;
    utils.http(httpUrl + "publishComment", {
      userid: userId, //	用户id
      messageid: that.data.messageid,//美食id
      code: codeName,//code值
      content: that.data.valueName, //	评论内容
      username: that.data.nickname,// 发布的用户的名字
      parentid: that.data.parentid,  //评论的ID
      parentuserid: that.data.parentuserid,
      parentusername: that.data.username
    }, function (res) {
      console.log(res);
      that.getlodeTap();
    })






  },
  //点击回复信息小图标
  oNinfonumTap: function (e) {
    var messageid = e.currentTarget.dataset.messageid;
    var nickname = e.currentTarget.dataset.nickname;
    var type = e.currentTarget.dataset.type;
    this.setData({
      ishis: true,
      messageid: messageid,
      nickname: nickname,
      type: type,
      valueName: "",
      username: "",
    })
  },

  //点击不同用户回复信息
  onchangeTap: function (e) {
    var that = this;
    var messageid = e.currentTarget.dataset.messageid;
    var parentusername = e.currentTarget.dataset.parentusername;
    var nickname = e.currentTarget.dataset.nickname;
    var parentid = e.currentTarget.dataset.parentid;
    var parentuserid = e.currentTarget.dataset.parentuserid;
    var username = e.currentTarget.dataset.username;   //要回复的username
    var type = e.currentTarget.dataset.type;
    var userid = e.currentTarget.dataset.userid;
    var id = e.currentTarget.dataset.id;
    if (parentuserid == undefined) {
      that.setData({
        parentid: id,
        parentuserid: userid,
      })
    } else {
      that.setData({
        parentid: parentid,
        parentuserid: parentuserid,
      })
    }

    this.setData({
      ishis: true,
      messageid: messageid,
      parentusername: parentusername,
      nickname: nickname,
      type: type,
      username: username,
      valueName: ""
    })
  },

  //提交回复
  bindsubmit: function (e) {
    var that = this;
    if (that.data.type == 1) {
      utils.http(httpUrl + "publishComment", {
        userid: userId, //	用户id
        messageid: that.data.messageid,//美食id
        code: codeName,//code值
        content: that.data.valueName, //	评论内容
        username: that.data.nickname  // 发布的用户的名字
      }, function (res) {
        console.log(res);
        that.getlodeTap();
      })
    } else {
      utils.http(httpUrl + "publishComment", {
        userid: userId, //	用户id
        messageid: that.data.messageid,//美食id
        code: codeName,//code值
        content: that.data.valueName, //	评论内容
        username: that.data.nickname,// 发布的用户的名字
        parentid: that.data.parentid,  //评论的ID
        parentuserid: that.data.parentuserid,
        parentusername: that.data.username
      }, function (res) {
        console.log(res);
        that.getlodeTap();
      })
    }
  },
  //滑动键盘收起
  handletouchmove: function (e) {
    this.setData({
      ishis: false,
    })
  },

})